package com.hazardSoft.eStore.ejb;

import java.util.LinkedList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.hazardSoft.eStore.bussines.ShoppingCarBO;
import com.hazardSoft.eStore.common.exception.SystemException;
import com.hazardSoft.eStore.entities.Bill;
import com.hazardSoft.eStore.entities.Sale;
import com.hazardSoft.eStore.interfaces.ShoppingCarLocal;

/**
 * Session Bean implementation class ShoppingCarEJB
 */
@Stateful
public class ShoppingCarEJB implements ShoppingCarLocal {

	public static final String EJB_NAME = ShoppingCarEJB.class.getSimpleName()+"Local";
	
	@PersistenceContext(unitName = "EStoreJPA")
	private EntityManager entityManager;
	private ShoppingCarBO shoppingCarBO;
//	private List<Sale> sales;
    /**
     * Default constructor. 
     */
    public ShoppingCarEJB() {
        // TODO Auto-generated constructor stub
    }

    @PostConstruct
    public void construct(){
    	List<Sale> sales = new LinkedList<Sale>();
    	shoppingCarBO = new ShoppingCarBO(sales);
    }
    
	/**
     * @throws SystemException 
	 * @see ShoppingCarLocal#addProduct(Sale)
     */
    public void addProduct(Sale sale) throws SystemException {
        shoppingCarBO.addProduct(sale);
    }

	/**
     * @throws SystemException 
	 * @see ShoppingCarLocal#removeProduct(Sale)
     */
    public void removeProduct(Sale sale) throws SystemException {
        shoppingCarBO.removeProduct(sale);
    }
    
	/**
     * @throws SystemException 
	 * @see ShoppingCarLocal#listProducts()
     */
    public List<Sale> listProducts() throws SystemException {
        
		return shoppingCarBO.listProducts();
    }

	/**
     * @throws SystemException 
	 * @see ShoppingCarRemote#setProducts(List<Sale>)
     */
    public void setProducts(List<Sale> sales) throws SystemException {
    	shoppingCarBO.setProducts(sales);  	
    }

	/**
     * @throws SystemException 
	 * @see ShoppingCarLocal#buyProducts(Bill)
     */
    public Bill buyProducts(Bill bill) throws SystemException {
        
		return shoppingCarBO.buyProducts(bill, entityManager);
    }

}
